import type { IAccount } from "@/types"
import syRequest from ".."
import { setAccessToken, setRefreshToken } from "../Authority/cacheToken"
import { ACCESS_TOKEN, REFRESH_TOKEN } from "@/global/constants"

export function accountLogin(account: IAccount) {
  return syRequest.post({
    url: "/login",
    data: account,
    interceptors: {
      responseSuccessFn(res) {
        // console.log("单次响应成功", res.headers.token)
        // 本地缓存token
        // console.log(res.headers)

        if (res.data.code == 200) {
          // console.log(res.headers)

          const accessToken = res.headers[ACCESS_TOKEN]
          const refreshToken = res.headers[REFRESH_TOKEN]

          setAccessToken(accessToken)
          setRefreshToken(refreshToken)
          // const token = res.headers.token
          // token && localCache.setCache(LOGIN_TOKEN, token)
        }
        return res
      }
    }
  })
}

export function userRegister(account: IAccount) {
  return syRequest.post({
    url: "/users/register",
    data: account
  })
}

/**
 * 测试鉴权
 * @returns
 */
export function authentication() {
  return syRequest.get({
    url: "/login/test"
  })
}
